﻿using System.Collections.Generic;

/// <summary>
/// 批量更新辅助筛选
/// </summary>
public class ListCompareHelper<T>
{
    /// <summary>
    /// 原集合
    /// </summary>
    private List<T> _orignal;

    /// <summary>
    /// 新集合
    /// </summary>
    private List<T> _current;

    /// <summary>
    /// Initializes a new instance of the <see cref="ListCompareHelper&lt;T&gt;"/> class.
    /// </summary>
    /// <param name="orignal">The orignal.</param>
    /// <param name="current">The current.</param>
    private ListCompareHelper(List<T> orignal, List<T> current)
    {
        this._orignal = orignal;
        this._current = current;
    }

    /// <summary>
    /// 初始化要比较的两个集合
    /// </summary>
    /// <param name="orignal">The orignal.</param>
    /// <param name="current">The current.</param>
    /// <returns></returns>
    public static ListCompareHelper<T> CompareInit(List<T> orignal, List<T> current)
    {
        return new ListCompareHelper<T>(orignal, current);
    }

    /// <summary>
    /// 获取废弃部分
    /// </summary>
    /// <returns></returns>
    public virtual List<T> GetInvalidList()
    {
        List<T> inter = GetIntersectsList();
        if (inter.Count == 0) return this._orignal;
        else
        {
            T[] arr = new T[this._orignal.Count];
            this._orignal.CopyTo(arr);
            List<T> li = new List<T>(arr);
            li.RemoveAll(delegate(T x) { return inter.Contains(x); });
            return li;
        }
    }

    /// <summary>
    /// 获取新增部分
    /// </summary>
    /// <returns></returns>
    public virtual List<T> GetValidList()
    {
        List<T> inter = GetIntersectsList();
        if (inter.Count == 0) return this._current;
        else
        {
            T[] arr = new T[this._current.Count];
            this._current.CopyTo(arr);
            List<T> li = new List<T>(arr);
            li.RemoveAll(delegate(T x) { return inter.Contains(x); });
            return li;
        }
    }

    /// <summary>
    /// 取交集
    /// </summary>
    /// <returns></returns>
    public virtual List<T> GetIntersectsList()
    {
        //List<T> inter;

        return this._orignal.Count >= this._current.Count
            ? this._orignal.FindAll(delegate(T x) { return this._current.Contains(x); })
            : this._current.FindAll(delegate(T x) { return this._orignal.Contains(x); });
    }

    /// <summary>
    /// 判断两集合是否完全相同，如使用对象，需对该对象的Equals方法进行重写
    /// </summary>
    /// <returns>
    ///   <c>true</c> if this instance is equal; otherwise, <c>false</c>.
    /// </returns>
    public virtual bool isEqual()
    {
        if (this._orignal.Count == this._current.Count)
        {
            bool flag = true;
            for (int i = 0; i < this._orignal.Count; i++)
            {
                if (this._orignal[i].Equals(this._current[i])) continue;
                else { flag = false; break; }
            }
            return flag;
        }
        else return false;
    }
}